package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.google.gson.Gson;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.LoginInfoBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.thirdlogin.ThirdLoginPlatform;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.ILoginView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.exception.ApiException;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/5/16.
 */

public class LoginPresenter extends BasePresenter<ILoginView> {

    public LoginPresenter(ILoginView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {
        HttpManager manager = new HttpManager(new HttpOnNextListener() {
            @Override
            public void onNext(String resulte, String mothead) {

            }

            @Override
            public void onError(ApiException e) {

            }
        }, rxAppCompatActivity);
        manager.doHttpDeal(baseApi);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void sendMsg(String phone, String type) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class).sendTemplateSMS(phone, type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {

                    }
                }));
    }

    public void normalLogin(String username, String password, String verifyCode) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class).logindoit(username, password, verifyCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<LoginInfoBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<LoginInfoBean> response) {
                        iView.loginSuccess(response.getData());
                    }
                }));
    }

    public void autoLogin(String token) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class).auto_login(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<LoginInfoBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<LoginInfoBean> response) {
                        iView.loginSuccess(response.getData());
                    }
                }));
    }

    public void thirdLogin(final Platform platform, final HashMap<String, Object> map) {
        String openId = platform.getDb().getUserId();
        String platformName = formatPlatformName(platform);
        map.put("access_token", platform.getDb().getToken());
        String type = "";
        switch (platformName) {
            case ThirdLoginPlatform.PLATFORM_QQ:
                type = "2";
                break;
            case ThirdLoginPlatform.PLATFORM_WECHAT:
                type = "1";
                break;
            case ThirdLoginPlatform.PLATFORM_SINA:
                type = "0";
                break;
        }
        Gson gson = new Gson();
        String data = gson.toJson(map);
        addSubscription(RetrofitSource.createApi(HttpPostService.class).san_login(openId, data, type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<LoginInfoBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<LoginInfoBean> response) {
                        iView.loginSuccess(response.getData());
                    }
                }));
    }

    @ThirdLoginPlatform
    private String formatPlatformName(Platform platform) {
        String platformName;
        if (platform instanceof SinaWeibo) {
            platformName = ThirdLoginPlatform.PLATFORM_SINA;
        } else if (platform instanceof QQ) {
            platformName = ThirdLoginPlatform.PLATFORM_QQ;
        } else if (platform instanceof Wechat) {
            platformName = ThirdLoginPlatform.PLATFORM_WECHAT;
        } else {
            throw new IllegalArgumentException(String.format("Unsupported platform %s!", platform));
        }
        return platformName;
    }

}
